package com.zbs.framework.swagger.utils;

import com.zbs.framework.swagger.annotations.ApiModelProperty;
import com.zbs.framework.swagger.entity.RequestParams;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FiledUtils {

    public static List<RequestParams> getField(Class<?> clazz) {
        List<RequestParams> requestParamsList = new ArrayList<>();
        Arrays.stream(clazz.getDeclaredFields()).forEach(field -> {
            field.setAccessible(true);
            ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
            RequestParams requestParams = new RequestParams();
            String typeName = field.getType().getName();
            requestParams.setName(field.getName()).setDescribe(field.getName()).setRequired(false).setArray(false).setDataType(typeName);
            if (apiModelProperty != null) {
                requestParams.setDescribe(apiModelProperty.value())
                        .setRequired(apiModelProperty.required());
            }
            requestParamsList.add(requestParams);
        });
        return requestParamsList;
    }
}
